<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      class PromiseZ {
        status = "pending";
        constructor(executor) {
          this.executor = executor;
          this.result = []; //存储结果
          this.cb = []; //存储异步结果
          this.executor(this.resolve.bind(this), this.reject.bind(this));
        }
        resolve(playload) {
          //   console.log("resolve",playload);
          if (this.status !== "pending") return; //状态凝固 确保执行一个状态
          this.status = "fullfilled";
          this.result = playload;
          this.cb.forEach((item) => {
            item.succCb && item.succCb(this.result);
          });
        }
        reject(playload) {
          //   console.log("reject");
          if (this.status !== "pending") return; //状态凝固 确保执行一个状态
          this.status = "reject";
          this.result = playload;
          this.cb.forEach((item) => {
            item.failCb && item.failCb(this.result);
          });
        }
        isPromiseZ(temp,resolve,reject){
          if (temp instanceof PromiseZ) {
                temp.then(
                  (res) => {
                    resolve(res);
                  },
                  (err) => {
                    reject(err);
                  }
                );
              } else {
                resolve(temp);
              }
        }
        catch(err){
     
          this.then(undefined,err)
        }
        then(succ, fail) {
          // if(!succ){
          //    succ=(succ)=>succ
          // }
      
          if(!fail){
             fail=(fail)=>fail
             console.log(22222);
          }
          return new PromiseZ((resolve, reject) => {
          
            if (this.status === "fullfilled") {
              var temp = succ && succ(this.result);
              this.isPromiseZ(temp,resolve,reject)
            }
            if (this.status === "reject") {
              var temp = fail && fail(this.result);
              this.isPromiseZ(temp,resolve,reject)
            }
            //   异步状态
            if (this.status === "pending") {
              this.cb.push({
                succCb: (val) => {
                  const temp = succ(val);
                  this.isPromiseZ(temp,resolve,reject)
                },
                failCb:(val) => {
                  const temp = fail(val);
                  this.isPromiseZ(temp,resolve,reject)
                },
              });
            }
          });
        }
      }

      const zks = new PromiseZ(function (resolve, reject) {
        // resolve("'我是resolve函数'");
        reject("'我是reject函数'");
        // setTimeout(() => {
        //   // resolve("'我是resolve函数'");
        //   reject("'我是reject函数'");
        // }, 1000);
      });

      zks
        .then(
          (res) => {
            console.log("success-----", res);
            // return "1111-11111111111";
            // return new PromiseZ((resolve, reject) => {
            //   // resolve("我是resolve22222222222函数");
            //   setTimeout(() => {
            //     resolve("我是resolve22222222222函数");
            //   }, 1000);
            // });
          },
          // (err) => {
          //   console.log("fail-----", err);
          //   // return "1111-11111111111";
          //   return new PromiseZ((resolve, reject) => {
          //     // reject("我是reject22222222222函数");
          //     setTimeout(() => {
          //       resolve("我是reject22222222222函数");
          //     }, 1000);
          //   });
          // }
        )
        .catch(err=>{
          console.log(err);
        })
        // .then(
        //   (res) => {
        //     console.log(res);
        //   },
        //   (err) => {
        //     console.log("fail-2222222----", err);
        //   }
        // );
    </script>
  </body>
</html>
